<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-05-14 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-27 12:08:33
 */

namespace app\weixin_manage\controller;
use app\common\controller\Backend;

class Menu extends Backend {

	private $buttonType = array(
		'click' => '点击推事件',
		'view' => '跳转URL',
		'scancode_push' => '扫码推事件',
		'scancode_waitmsg' => '扫码推事件且弹出“消息接收中”提示框',
		'pic_sysphoto' => '弹出系统拍照发图',
		'pic_photo_or_album' => '弹出拍照或者相册发图',
		'pic_weixin' => '弹出微信相册发图器',
		'location_select' => '弹出地理位置选择器',
		'miniprogram' => '小程序',
	);

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '微信菜单', '公众号管理', 'menu');

		$this->weixinAccountDao = model('WeixinAccount');
		$this->weixinMenuDao = model('weixin_manage/WeixinMenu');
	}

	function index() {
		if (!$this->wid) {
			$this->adminError('微信wid有误');
		}
		//获取菜单列表
		list($menuList) = $this->weixinMenuDao->search(['wid'=>$this->wid], '');
		//print_r($wxmenuList);exit;

		//列表转换成树
		$tree_obj = new \org\util\Tree;
		$menuTree = $tree_obj->toFormatTree($menuList, 'name', 'id', 'parentid');
		//print_r($menuTree);
		$this->assign('menuTree', $menuTree);
		
		return $this->fetch();
	}

	public function listorders() {
		$result = parent::baseListorders($this->weixinMenuDao);

		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');
		if ($id) {
			$info = $this->weixinMenuDao->getInfo($id);
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		//获取父级菜单列表
		list($categorys) = $this->weixinMenuDao->getCategorys($this->wid);
		//print_r($categorys);
		$this->assign('categorys', $categorys);

		$this->assign('buttonType', $this->buttonType);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');
		$keyword = $_POST['data']['keyword']; //key不过滤，但是感觉不是这种写法，先不管了
		$data['keyword'] = $keyword;
		//print_r($data);exit;

		// if ($data['event'] && !$data['key']) {
		//     $this->errorAlertAjax("您选择了按钮事件，请输入关键字。");
		// }

		$event = $data['event'];
		if ($event == 'view') {
			if (!preg_match('/(http:\/\/)|(https:\/\/)/i', $data['keyword'])) {
				$this->error("跳转url事件的关键词必须是超链接(http://开头或者https://开头)");
			}
		}

		// if ($data['parentid'] == 0 && $data['event'] == 'view') {
		//     $data['event'] = '';
		// }

		// if ($data['parentid'] == 0) {
		//     $data['event'] = $data['key'] = '';
		// }

		if ($id) {
			$result = $this->weixinMenuDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功', url('weixin_manage/menu/index', ['wid'=>$this->wid]));
			} else {
				$this->error('保存失败'.showDbError($this->weixinMenuDao));
			}
		} else {
			$data['wid'] = $this->wid;
			$result = $this->weixinMenuDao->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功', url('weixin_manage/menu/index', ['wid'=>$this->wid]));
			} else {
				$this->error('添加失败'.showDbError($this->weixinMenuDao));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');
		if (!$id) {
			$this->error('非法ID');
		}

		$result = $this->weixinMenuDao->del($id);
		if ($result !== false) {
			$this->success('删除成功', url('weixin_manage/menu/index', ['wid'=>$this->wid]));
		} else {
			$this->error('删除失败'.showDbError($this->weixinMenuDao));
		}
	}

	//更新至微信
	public function createMenu() {
		//微信jssdk
		$jssdk = new \org\weixin\jssdk($this->weixinAccountInfo['appid'], $this->weixinAccountInfo['appsecret'], $this->weixinAccountInfo['wxnumber']);
		$signPackage = $jssdk->getSignPackage();

		//获取缓存中的access_token
		$access_token = $jssdk->getAccessTokenForOut();
		
		$options = array(
			'token'=>$this->weixinAccountInfo['token'], //填写你设定的key
			'encodingaeskey'=>$this->weixinAccountInfo['aeskey'], //填写加密用的EncodingAESKey
			'appid'=>$this->weixinAccountInfo['appid'], //填写高级调用功能的app id, 请在微信开发模式后台查询
			'appsecret'=>$this->weixinAccountInfo['appsecret'], //填写高级调用功能的密钥
			'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
			'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
			'paysignkey'=>'' //商户签名密钥Key，支付权限专用
		);
		$weObj = new \wechat\Wechat($options);
		//使用jssdk获取到的access_token赋值，下文要用
		if ($access_token) {
			$weObj->checkAuth('', '', $access_token);
		}
		//$result = $weObj->valid(); //验证(这里不能加验证)

		$newmenu = $this->weixinMenuDao->getMenu($this->wid);
		//print_r($newmenu);exit;
		
		$result = $weObj->createMenu($newmenu);

		if ($result !== false) {
			$this->success('生成自定义菜单成功');
		} else {
			$this->error('生成菜单失败，请到公众平台里面生成，错误提示：'.$weObj->errMsg);
		}
	}
}
?>